/**
 * $Id$
 *
 * Copyright 2000-2004 Antonio W. Lagnada. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * 2. The name "Antonio W. Lagnada" must not be used to endorse or promote
 * products derived from this software without prior written permission. For
 * written permission, please contact tony@lagnada.com
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANTONIO W.
 * LAGNADA OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *  
 */
package org.j2ee.example.web.multiaction;

import static org.springframework.util.Assert.*;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContextException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.util.UrlPathHelper;

public class MultiActionControllerBase extends MultiActionController {

    protected UrlPathHelper m_urlPathHelper = new UrlPathHelper();

    public MultiActionControllerBase() throws ApplicationContextException {
        super();
    }

    public MultiActionControllerBase(Object delegate)
            throws ApplicationContextException {
        super(delegate);
    }

    public void init() {
        // Make sure that the Name Resolver is the proper type
        assertBeanProperty(super.getMethodNameResolver(),
                "The methodNameResolver is required and must be this type ["
                        + PropertiesMethodNameResolver.class + "].");
        isTrue(
                super.getMethodNameResolver() instanceof PropertiesMethodNameResolver,
                "The methodNameResolver must be of this type: ["
                        + PropertiesMethodNameResolver.class + "].");
    }

    protected String getSuccessView(HttpServletRequest request) {
        String path = m_urlPathHelper.getLookupPathForRequest(request);
        PropertiesMethodNameResolver resolver = (PropertiesMethodNameResolver) super
                .getMethodNameResolver();
        return resolver.getSuccessViewNameForUrlPath(path);
    }

    protected ModelAndView getModelAndView(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView(getSuccessView(request));
        return mv;
    }

    protected void assertBeanProperty(Object beanProperty, String name) {
        notNull(beanProperty, "Failed to initialize " + name);
    }
}
